/**
 * Created by xz on 2017/12/19.
 */
console.log('reportLostChild');
// 初始化table
var $table =  $("#reportLostChild_table").bootstrapTable({
    'url': '/friends/lostChild/findLostChildClueRecord',
    'queryParams': queryParams,
    'cache': false,
    'toolbar': $('div[data-name="btnCtl"]'),
    // 'toolbarAlign': 'right',
    'striped': true,
    'sidePagination': 'server',
    'pagination': true,
    'sortable': true, //是否启用排序
    'sortOrder': "asc", //排序方式
    'pageSize': globalPageSize,
    'pageList': [],
    'clickToSelect': false,
    'singleSelect': true,
    'maintainSelected': true,
    'smartDisplay': true,
    'ajaxOptions':{
        headers: {"Authorization": sessionStorage.token}
    },
    'responseHandler': function (resb) {
        return {
            'total': resb.data.total,
            'rows': resb.data.content
        }
    },
    'onCheck': function() {
        $("button[data-target='#deleteModal']").removeAttr("disabled");
    },
    'onUncheck': function() {
        $("button[data-target='#deleteModal']").attr("disabled", 'true');
    },
    // 'onPageChange': function () {
    //     if ($("#recommendFace_table").bootstrapTable('getSelections')[0]) {
    //         $("#recommendFace_table").bootstrapTable('uncheckBy', {
    //             field: 'playCount',
    //             values: [$("#recommendFace_table").bootstrapTable('getSelections')[0].playCount]
    //         });
    //     }
    // },
    'columns': [
        {
            'field': 'checkStatus',
            'checkbox': true
        },
        {
            'field': 'recordId',
            'title': 'ID',
            'align': 'center',
            'class': 'hide ID'
        },{
            'field': 'createDate',
            'title': '上报时间',
            'align': 'center'
        },
        {
            'field': 'no',
            'title': '寻亲编号',
            'align': 'center'
        },
        // {
        //     'field': 'sex',
        //     'title': '性别',
        //     'align': 'center'
        // },
        {
            'title': '操作',
            'align': 'center',
            'formatter': function (value, row) {
                var  html = "<button class='btn btn-sm btn-success detail' style='margin-right: 10px'>查看详情</button>";
                return html;
            }
        }]
});

//请求服务数据时所传参数
function queryParams(params){
    return {
        limit : params.limit, //每一页的数据行数，默认是上面设置的10(pageSize)
        offset : params.offset
    }
};

// //数据搜索
// $('#dataSearch').click(function () {
//     search();
// });
//
// function search() {
//     $.ajaxSetup({cache:false});
//     $.ajax({
//         type: "POST",
//         url: "/friends/lostChild/findLostChildClueRecord",
//         data: {
//             limit : 15, //每一页的数据行数，默认是上面设置的10(pageSize)
//             offset : 0
//         },
//         dataType: 'json',
//         beforeSend: function (request) {
//             request.setRequestHeader("Authorization", sessionStorage.token);
//         },
//         success: function (resb) {
//             if (resb.data.content.length > 0) {
//                 $("#reportLostChild_table").bootstrapTable('load', {
//                     'total': resb.data.total,
//                     'rows': resb.data.content
//                 });
//             } else {
//                 $("#reportLostChild_table").bootstrapTable('removeAll');
//             }
//         },
//         error: function (XMLHttpRequest, textStatus, errorThrown) {
//             alert(textStatus);
//         }
//     });
// }

//数据删除
$('#confirmDelete').click(function () {
    var tableData = $("#reportLostChild_table").bootstrapTable("getSelections")[0];
    console.log(tableData)
    deleteDate(tableData.recordId);
});

function deleteDate(id) {
    $.ajaxSetup({cache:false});
    $.ajax({
        type: "GET",
        url: "/friends/lostChild/deleteLostChild?id="+id,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", sessionStorage.token);
        },
        success: function (resb) {
            $("#reportLostChild_table").bootstrapTable('refresh');
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

/**
 * 详情模态框显示
 */
$('#reportLostChild_table').on('click', '.detail', function () {
    var id = $(this).parent().siblings('.ID').html();
    $.ajaxSetup({cache:false});
    $.ajax({
        type: "GET",
        url: "/friends/lostChild/getLostChildClueDetail?recordId="+id,
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", sessionStorage.token);
        },
        success: function (resb) {
            if(resb.success) {
                var data = resb.data;
                $('#photoUrl').attr('src', data.lostChild.photoUrl);
                $('#no').html(data.lostChild.no);
                $('#name').html(data.lostChild.name);
                $('#sex').html(data.lostChild.sex);
                $('#birthday').html(data.lostChild.birthday);
                $('#height').html(data.lostChild.height);
                $('#currentPlace').html(data.lostChild.currentPlace);
                $('#missingDate').html(data.lostChild.missingDate);
                $('#description').html(data.lostChild.description);
                $('#otherData').html(data.lostChild.otherData);
                $('#createDate').html(data.lostChild.createDate);
                $('#reportName').html(data.lostChildClueRecord.userName);
                $('#reportPhone').html(data.lostChildClueRecord.userPhoneNo);
                $('#reportContent').html(data.lostChildClueRecord.message);
                var imgArr = data.lostChildClueRecord.photoUrl.split(',');
                $('#reportImageList').empty();
                for(var i = 0; i < imgArr.length; i++) {
                    $('#reportImageList').append("<p style='text-align: center'><img style='height: 200px;' src="+ imgArr[i] +"></p>");
                }
                // $('#reportImageList').html()
                $('#detailModal').modal('show');
            }else{
                alert('未找到详细信息');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
});

//返回上一级页面
$('#back').click(function () {
    loadPage('recommendFace/recommendFace.html');
});
